<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">

  <title>count</title>
  <link href="../cppreference.css" rel="stylesheet" type="text/css">
</head>

<body>
<table>
  <tr>
  <td>
  <div class="body-content">

  <div class="header-box">
    <a href="../index.html">cppreference.com</a> &gt; <a href=
    "index.html">C++ Algorithms</a> &gt; <a href="count.html">count</a>
  </div>

  <div class="name-format">
    count
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;algorithm&gt;
  size_t count( iterator start, iterator end, const <a href=
"../containers.html">TYPE</a>&amp; val );
</pre>

  <p>The count() function returns the number of elements between
  <em>start</em> and <em>end</em> that match <em>val</em>.</p>

  <p>For example, the following code uses count() to determine how many
  integers in a vector match a target value:</p>
  <pre class="example-code">
 vector&lt;int&gt; v;
 for( int i = 0; i &lt; 10; i++ ) {
   v.push_back( i );
 }              

 int target_value = 3;
 int num_items = count( v.begin(), v.end(), target_value );             

 cout &lt;&lt; &quot;v contains &quot; &lt;&lt; num_items &lt;&lt; &quot; items matching &quot; &lt;&lt; target_value &lt;&lt; endl;            
</pre>

  <p>The above code displays the following output:</p>
  <pre class="example-code">
 v contains 1 items matching 3          
</pre>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="accumulate.html">accumulate</a><br>
    <a href="adjacent_difference.html">adjacent_difference</a><br>
    <a href="count_if.html">count_if</a><br>
    <a href="inner_product.html">inner_product</a><br>
    <a href="partial_sum.html">partial_sum</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
